const { Controller } = require('egg')

class UploadController extends Controller {
  // 文件上传
  async upload() {
    const { ctx, service } = this
    const file = ctx.request.files
    let Location = ''
    if (!file.length === 0) {
      return ctx.apiFail('请上传文件')
    } else if (file.length === 1) {
      //  单文件
      try {
        Location = await service.upload.upload(file[0])
        return ctx.apiSuccess(`http://${Location.Location}`)
      } catch {
        return ctx.apiFail('文件上传失败')
      }
    }
  }
}

module.exports = UploadController
